/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
 */

package res.elements;

import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.collision.*;
import org.jbox2d.collision.shapes.*;
import plar.core.Action;
import plar.core.ActionLifeTimer;
import plar.core.ActionTimer;
import plar.core.ElementBullet;
import plar.core.Common;
import plar.core.Element;
import plar.core.SpriteSet;
import java.util.ArrayList;
import plar.core.Sprite;
import plar.core.Common;

import plar.core.ElementPlayer;
import plar.core.Element;
import plar.core.Common.ElementType;
import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Fixture;

public class Mine  extends ElementBullet {
    public float explosionTime;
    public AABB explosionRelativeRectangle;
    public int explosionDamage;

    public Mine() {
        super();
        currentSpriteSet = new SpriteSet();
        currentSpriteSet.autoSize = false;
        Common.info(1, "nuova mina!");
        explosionTime = 5;
        lifeTime = 6;
        ActionLifeTimer alt = new ActionLifeTimer();
        actions.addAction("alt", alt);
        actions.addAction("mine", new ActionTimer("",explosionTime,0,false) {


            public Object force(Object o) {

                Mine mme = (Mine) me;
                AABB rr = new AABB(mme.explosionRelativeRectangle);
                Vec2 c = me.body.getWorldCenter();
                AABB ar = new AABB(new Vec2(c.x + rr.lowerBound.x/2, c.y
                                            + rr.lowerBound.y/2), new Vec2(c.x + rr.upperBound.x/2,
                                                    c.y + rr.upperBound.y/2)

                                  );

                ArrayList<Fixture>  ss =  Common
                                          .queryAABB(level.world, ar);


                currentSpriteSet = new SpriteSet();
                currentSpriteSet.autoSize=false;
                currentSpriteSet.add(mme.explosionSprite);
                //  Common.info(1,"sprite is now:"+ currentSpriteSet.get(0).getFileName());
                for (Fixture s : ss) {
                    Element p = (Element) s.getBody().getUserData();

                    if (p.type==Common.ElementType.PLAYER) {

                        ((ElementPlayer) p).energy -= explosionDamage;
                        Common.info(5, "shooted to:" + p);
                    }
                }
                return null;

            }



        });
    }
    Sprite explosionSprite;
    public void setBody(Body b)
    {   super.setBody(b);
        Common.info(1,"currentSETBOFY");
        currentSpriteSet = new SpriteSet();

        Sprite mineSprite = new Sprite("res/elements/mine.png", explosionTime);
        mineSprite.sizex = getSize().x;
        mineSprite.sizey = getSize().y;
        explosionSprite = new Sprite("res/elements/explosion0.png", lifeTime-explosionTime);
        Common.info(1,"explosion estentss x:"+explosionRelativeRectangle.getExtents().x);
        explosionSprite.sizex = explosionRelativeRectangle.getExtents().x;
        explosionSprite.sizey = explosionRelativeRectangle.getExtents().y;
        currentSpriteSet = new SpriteSet();
        currentSpriteSet.add(mineSprite);
        //currentSpriteSet.add(explosionSprite);

        currentSpriteSet.autoSize=false;
    }

}
